package com.pickgliss.bitmapEngin.ds
{
	import com.pickgliss.bitmapEngin.cache.CacheClip;
	import com.pickgliss.ui.core.Disposeable;
	public class CacheClipList implements Disposeable
	{
		public function CacheClipList()
		{
			head = tail = null;
			_count = 0;
		}
		
		public var head:CacheClip;
		public var tail:CacheClip;
		protected var _count:int;
		
		public function append($clip:CacheClip):void
		{
			if ($clip.parentList)throw (new Error("影片已经存在某一个列表中")) ;
			if (head)
			{
				tail.next = $clip; 
				$clip.prev = tail;
				tail = $clip;
			}
			else
			{
				head = tail = $clip;
			}
			$clip.parentList = this;
			++_count;
		}
		
		public function dispose():void
		{	
			var clip:CacheClip;
			while (head)
			{
				clip = head;
				head = head.next;
				if (head)head.prev	= null;
				clip.next = null;
				clip.parentList = null;
				--_count;
			}
			head = null;
			tail = null;			
		}
		
		public function get length():int { return _count; }
		
		public function prepend($clip:CacheClip):void
		{
			if ( $clip.parentList )throw (new Error("影片已经存在某一个列表中")) ;
			if (head)
			{
				head.prev	= $clip;
				$clip.next = head;
				head = $clip;
			}
			else
			{
				head = tail = $clip;
			}
			$clip.parentList = this;
			++_count;
		}
		
		public function remove($clip:CacheClip):CacheClip
		{
			if ($clip.parentList != this)throw (new Error("影片已经存在某一个列表中"));
			if ($clip == head)
			{
				head = $clip.next;
				if(head) head.prev	= null;
			}
			else
			{
				$clip.prev.next = $clip.next;
			}
			if($clip == tail)
			{
				tail = $clip.prev;
				if (tail) tail.next = null;
			}
			else
			{
				$clip.next.prev = $clip.prev;
			}
			$clip.prev = null;
			$clip.next = null;
			$clip.parentList = null;
			--_count;
			return $clip;
		}
		
		public function removeHead():CacheClip
		{
			var clip:CacheClip = head;
			if (head)
			{
				clip = head;
				head = clip.next;
				if (head) head.prev = null;
				--_count;
				clip.next = null;
				clip.parentList = null;
				return clip;
			}
			return null ;
		}
		
		public function removeTail():CacheClip
		{
			var clip:CacheClip = tail;
			if (tail)
			{
				tail = tail.prev;
				if (tail) tail.next = null;
				--_count;
				
				clip.prev = null;
				clip.parentList = null;
				return clip;
			}
			return null;
		}
		
		
		public function toString():String
		{
			var output:String,
			clip:CacheClip,
			count:int = 0;
			output = " List Content ( "+ _count + " elems ) : \n";
			if (! head)
			{
				output += "\t vide";
				return output;
			}
			clip = head.next;
			output += "\t head = " + (head? "ID:" + head.id : head) + (head == tail ? " = tail " : "" ) + (head.y ? "(y=" + head.y+ ")" : "" ) + "\n";
			output += "\t\t\t prev = " + (head.prev? "ID:" + head.prev.id : head.prev) + "\n";
			output += "\t\t\t next = " + (head.next? "ID:" + head.next.id : head.next) + "\n";
			++count;
			while (clip && (clip != tail))
			{
				output += "\t clip " + (++count) + " = " + (clip? "ID:" + clip.id : clip) + (clip.y ? "(y=" + clip.y+ ")" : "" ) + "\n";
				output += "\t\t\t prev = " + (clip.prev? "ID:" + clip.prev.id : clip.prev) + "\n";
				output += "\t\t\t next = " + (clip.next? "ID:" + clip.next.id : clip.next)+ "\n";
				
				clip = clip.next;
			}
			if (head != tail)
			{
				output += "\t tail = " +  (tail? "ID:" + tail.id : tail)  + (tail.y ? "(y=" + tail.y+ ")" : "" ) + "\n";
				output += "\t\t\t prev = " + (tail.prev? "ID:" + tail.prev.id : tail.prev) + "\n";
				output += "\t\t\t next = " + (tail.next? "ID:" + tail.next.id : tail.next) + "\n";
			}
			return output;
		}
	}
}